<template>
	<view class="order">
		<navigator url="../mine/address/address" class="p_box">
			<view class="pay_status">
				等待支付
			</view>
			<view class="address">
				<view class="name">
					{{ address.name }}
				</view>
				<view class="address-info">
					<view class="tel">
						tel：{{ address.tel }}
					</view>
					<view class="add-detail">
						{{detailAddress}}
					</view>
				</view>
			</view>
		</navigator>
		<view class="order-detail">
			<view class="order-t">
				<view class="bh">
					订单编号：
				</view>
				<text>等待支付</text>
			</view>
			<view class="order-item" v-for="item in payList">
				<image :src="item.cover_img" mode=""></image>
				<view class="goods-info">
					<view class="info-t">
						<text class="title">{{ item.gname }}</text>
						<text class="title">￥{{ item.price_now }}</text>
					</view>
					<view class="info-d">
						<view class="detail">
							<view class="sku">
								编号：{{item.pcode}}
							</view>
							<view class="classess">
								规格：{{ item.color_text }}
							</view>
						</view>
						<view class="count">
							x {{item.count}}
						</view>
					</view>
				</view>
			</view>
			<view class="pay">
				<view class="pay-con">
					<text>合计：{{totalPrice}}</text>
					<view class="confirm" @tap="pay">
						确认付款
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { mapState } from 'vuex';
	import { addOrderBatch } from '../../utils/order/index.js';
	export default {
		data() {
			return {
				address: {},
				detailAddress: '',
				addressId: ''
			};
		},
		computed: {
			...mapState(['payList']),
			totalPrice () {
				console.log(this.payList);
				return this.payList.reduce((acc, item) => {
						return acc + item.count * item.price_now;
				},0)
			}
		},
		methods: {
		async	pay () {
				let userId = uni.getStorageSync('userInfo').id;
				let addressId = this.addressId;
				let count = this.payList.reduce((acc, item) => {
					return acc + item.count;
				}, 0)
				let freight = '12';
				// let freight = '12.00';
				// let price = this.totalPrice + '.00';
				let price = this.totalPrice;
				let obj = {};
				obj.userId = userId;
				obj.addressId = addressId;
				obj.count = count;
				obj.freight = freight;
				obj.price = price;
				let orderParams = {};
				for (let i =0; i < this.payList.length; i++) {
					orderParams[`orderDetail[${i}].count`] = this.payList[i].count;
					orderParams[`orderDetail[${i}].skuId`] = this.payList[i].sku_id;
					orderParams[`orderDetail[${i}].price`] = this.payList[i].price_now;
				}
				let orderInfo = Object.assign(obj, orderParams);
				console.log('测试是是是是',orderInfo);
				let data = await addOrderBatch(orderInfo);
				console.log(data);
				if (data.data.code == 'S') {
					uni.setStorageSync('orderId', data.data.orderId);
					uni.setStorageSync('totalPrice', this.totalPrice);
					// 删除购物车中的数据
					let carList = uni.getStorageSync('shopCart');
					this.payList.forEach(el => {
						let index = carList.findIndex(item => item.id == el.id);
						if (index > -1) {
							carList.splice(index, 1);
						}
					})
					this.$store.commit('setPayList', []);
					uni.setStorageSync('shopCart', carList);
					uni.navigateTo({
						url: './pay/pay'
					})
				}
				
			}
		},
		onShow() {
			uni.setNavigationBarTitle({
			　　title:'乐居'
			});
			console.log(this.payList);
			this.address = uni.getStorageSync('address');
			this.detailAddress = this.address.provinceStr + this.address.cityStr + this.address.areaStr;
			this.addressId = this.address.id;
		}
	}
</script>

<style lang="scss">
	.order {
		width: 100%;
		min-height: 100vh;
		max-height: 100%;
		background-color: #F1ECE7;
		.p_box {
			box-sizing: border-box;
			width: 100%;
			padding: 0 50rpx;
			.pay_status, .address {
				border-bottom: 1px solid #e5e5e5;
			}
			.pay_status {
				font-size: 36rpx;
				padding: 20rpx 0;
			}
			.address {
				display: flex;
				align-items: center;
				padding: 30rpx 0;
				color: #494949;
				.name{
					font-size: 28rpx;
					margin-right: 20rpx;
				}
				.address-info{
					font-size: 28rpx;
				}
			}
		}
		
		.order-detail {
			margin: 35rpx;
			background-color: #fff;
			border-radius: 20rpx;
			box-sizing: border-box;
			padding: 30rpx;
			.order-t {
					display: flex;
					justify-content: space-between;
					font-size: 22rpx;
					color: #333;
					padding-bottom: 25rpx;
					border-bottom:1px solid #F1ECE7;
			}
			.order-item {
				display: flex;
				padding: 20rpx;
				border-bottom:1px solid #F1ECE7;
				image {
					width: 160rpx;
					height: 160rpx;
					margin-right: 20rpx;
				}
				.goods-info {
					flex: 1;
					.info-t, .info-d {
						display: flex;
						justify-content: space-between;
						font-size: 26rpx;
						// font-weight: bolder;
						color: #3E3E3E;
						letter-spacing: 1.86rpx;
					}
					.info-t {
						margin: 20rpx 0;
					}
					.info-d {
						color: #8d8d8d;
					}
				}
			}
		}
		.pay {
			display: flex;
			justify-content: flex-end;
			font-size: 26rpx;
			margin-top: 20rpx;
			.confirm {
				margin-top: 20rpx;
				width: 128rpx;
				height: 50rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				border: 1px solid #000;
				border-radius: 10rpx;
				
			}
		}
	}
</style>
